package com.example.composemodifiterdemo

import android.content.ContentValues.TAG
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.*
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme.typography
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp

class MainActivity : ComponentActivity() {
    private val backgroundShape: Shape = RoundedCornerShape(4.dp)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            Image(modifier = Modifier)
            Button(modifier = Modifier)
            background(modifier = Modifier)
            //fillMaxSize(modifier = Modifier)
            //padding(modifier = Modifier)
            //offSet(modifier = Modifier)
            weightModifierDemo(modifier = Modifier)
        }
    }

    @Composable
    fun Image(modifier: Modifier) {
        Row {
            Image(
                painterResource(id = R.mipmap.iv_pic),
                contentDescription = stringResource(R.string.description),
                modifier = modifier
                    .size(60.dp)//宽和高同时设置成60
                    .clip(CircleShape)//将图片裁剪成圆形
            )
            Spacer(Modifier.width(20.dp))
            Image(
                painterResource(id = R.mipmap.iv_pic),
                contentDescription = stringResource(R.string.description),
                modifier = Modifier
                    .size(width = 100.dp, height = 100.dp)
                    .clip(CircleShape)
            )

        }
    }

    @Composable
    fun Button(modifier: Modifier) {
        Row {
            Spacer(modifier.width(200.dp))
            Text(
                text = stringResource(R.string.description),
                style = typography.bodySmall.copy(color = Color.White),
                textAlign = TextAlign.Center,
                modifier =
                Modifier
                    .width(80.dp)
                    .clickable(onClick = {})
                    .shadow(3.dp, shape = backgroundShape)
                    .clip(backgroundShape)
                    .background(
                        brush = Brush.verticalGradient(
                            colors = listOf(
                                Color.Red,
                                Color.Blue,
                            ),
                            startY = 0f,
                            endY = 80f
                        )
                    )
                    .padding(vertical = 10.dp)
                    .pointerInput(Unit){
                        detectTapGestures(
                            onPress = {
                                Log.i(TAG, "onTouch ")      // 触摸事件
                            }, onTap = {
                                Log.i(TAG, "onClick ")      // 点击事件
                                intent = Intent(this@MainActivity,BaseWeightActivity::class.java)
                                startActivity(intent)
                            }, onDoubleTap = {
                                Log.i(TAG, "onDoubleClick ")// 双击事件
                            }, onLongPress = {
                                Log.i(TAG, "onLongClick ")  // 长按事件
                            })
                    })
        }
    }

    @Composable
    fun background(modifier: Modifier) {
        Row {
            Spacer(Modifier.width(300.dp))
            Box(
                modifier = modifier
                    .size(100.dp)
                    .background(color = Color.Red)
            )
            {
                Text(text = "纯色", modifier.align(Alignment.Center))
            }
            Spacer(Modifier.width(30.dp))
            Box(
                modifier
                    .size(150.dp)
                    .background(brush = verticalGradientBrush)
            )
            {
                Text(text = "渐变色", modifier.align(Alignment.Center))
            }
        }
    }

    @Composable
    fun fillMaxSize(modifier: Modifier) {
        Box(
            modifier = modifier
                .fillMaxSize()//宽高同时铺满屏幕
                .background(Color.Red)
        )
        Box(
            modifier = modifier
                .fillMaxHeight()
                .width(60.dp)//高度铺满屏幕
                .background(Color.Blue)
        )
        Box(
            modifier = modifier
                .fillMaxWidth()
                .height(60.dp)
                .background(Color.Green)
        )//宽度铺满屏幕
    }

    @Composable
    fun padding(modifier: Modifier) {
        Box(
            modifier = modifier
                .padding(8.dp)
                .border(2.dp, Color.Red, shape = RoundedCornerShape(2.dp))
                .padding(8.dp)

        )
          {
                Spacer(
                    modifier = modifier
                        .size(width = 400.dp, height = 20.dp)
                        .background(Color.Red)
                )
            }
    }

    @Composable
    fun offSet(modifier: Modifier){
        Row {
            Spacer(modifier = Modifier.width(440.dp))
            Box(
                modifier
                    .size(100.dp)
                    .offset(x = 200.dp, y = 150.dp)
                    .background(Color.Red)
            )
            Box(modifier
                .size(100.dp)
                .offset
                { IntOffset(200.dp.roundToPx(), 150.dp.roundToPx()) }
                .background(Color.Red))
        }
    }

    @Composable
    fun matchParentModifierDemo(){
        Box {
            Box(modifier = Modifier
                .matchParentSize()
                .background(Color.Red))
            userInfo()
        }
    }
    
    @Composable
    fun weightModifierDemo(modifier : Modifier){
        Row {
            Spacer(Modifier.width(620.dp))
            Column(
                modifier = Modifier
                    .width(300.dp)
                    .height(200.dp)
            ) {
                Box(
                    modifier = modifier
                        .weight(1f)
                        .fillMaxWidth()
                        .background(Color.Green)
                )
                Box(
                    modifier = modifier
                        .weight(1f)
                        .fillMaxWidth()
                        .background(Color.Blue)
                )
                Box(
                    modifier = modifier
                        .weight(1f)
                        .fillMaxWidth()
                        .background(Color.Red)
                )
            }
        }
    }

    private fun userInfo() {

    }

    //创建brush渐变色
    private val verticalGradientBrush = Brush.verticalGradient(
        colors = listOf(
            Color.Red,
            Color.Green,
            Color.Blue
        ),
    )
}

